﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Microsoft.Phone.Controls;
using System.ComponentModel;
using System.Runtime.Serialization.Json;
using System.Windows.Navigation;
using System.Windows.Media.Imaging;
using System.IO.IsolatedStorage;
using System.IO;

namespace DriveJoy
{
    public partial class MainPortrait : PhoneApplicationPage
    {
        public MainPortrait()
        {
            InitializeComponent();
            
        }

        public IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        public string curDir = "";
        public List<ItemStructure> listItem = new List<ItemStructure>();

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            // This part of the code sets up a few directories and files in the 
            // store.                                             
            isoStore = IsolatedStorageFile.GetUserStoreForApplication();

            // Create directory
            isoStore.CreateDirectory("TopLevelDirectory");
            isoStore.CreateDirectory("TopLevelDirectory/SecondLevel");
            isoStore.CreateDirectory("TopLevelDirectory/SecondLevel/ThirdLevel");
            isoStore.CreateDirectory("TopLevelDirectory/SecondLevel/ThirdLevel/FourthLevel");
            isoStore.CreateDirectory("TopLevelDirectory/SecondLevel/ThirdLevel/Four1");
            isoStore.CreateDirectory("TopLevelDirectory/SecondLevel/ThirdLevel/Four2");
            isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory");
            isoStore.CreateFile("InTheRoot.txt");
            isoStore.CreateFile("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt");
            // End of setup.

            try
            {
                GetPattern("*", isoStore, listItem);

                listFile.ItemsSource = listItem;
            }
            catch (Exception) 
            { 
            }

            

            //listFile.Items.Clear();
            //foreach (ItemStructure item in listItem)
            //{
            //    listFile.Items.Add(item.Name);
            //}
            //foreach (string directory in GetAllDirectories("*", isoStore))
            //{
            //    this.listFile.Items.Add(directory);
            //}
            //foreach (string file in GetAllFiles("*", isoStore))
            //{
            //    this.listFile.Items.Add(file);
            //}

        }
        #region Isolation Storage
        // Method to retrieve all directories, recursively, within a store. 
        public List<ItemStructure> GetPattern(string pattern, IsolatedStorageFile storeFile, List<ItemStructure> listItem)
        {
            //List<ItemStructure> listItem = new List<ItemStructure>();
            GetAllDirectories(pattern, storeFile, listItem);
            GetAllFiles(pattern, storeFile, listItem);

            return listItem;
        }
        public static void GetAllDirectories(string pattern, IsolatedStorageFile storeFile,List<ItemStructure> listItem)
        {
            // Get the root of the search string. 
            string root = Path.GetDirectoryName(pattern);

            if (root != "")
            {
                root += "/";
                ItemStructure item = new ItemStructure();
                item.Name = "..";
                item.Type = ItemType.Up;
                listItem.Add(item);
            }

            // Retrieve directories.
            List<String> directoryList = new List<String>(storeFile.GetDirectoryNames(pattern));
            foreach (string s in directoryList)
            {
                ItemStructure item = new ItemStructure();
                item.Name = s;
                item.Image = "Image/doc-icon.png";
                item.Type = ItemType.Folder;
                listItem.Add(item);
            }
            // Retrieve subdirectories of matches. 
            //for (int i = 0, max = directoryList.Count; i < max; i++)
            //{
            //    string directory = directoryList[i] + "/";
            //    List<String> more = GetAllDirectories(root + directory + "*", storeFile);

            //    // For each subdirectory found, add in the base path. 
            //    for (int j = 0; j < more.Count; j++)
            //    {
            //        more[j] = directory + more[j];
            //    }

            //    // Insert the subdirectories into the list and 
            //    // update the counter and upper bound.
            //    directoryList.InsertRange(i + 1, more);
            //    i += more.Count;
            //    max += more.Count;
            //}

            //return directoryList;
        }

        public static void GetAllFiles(string pattern, IsolatedStorageFile storeFile, List<ItemStructure> listItem)
        {
            // Get the root and file portions of the search string. 
            string fileString = Path.GetFileName(pattern);

            List<String> fileList = new List<String>(storeFile.GetFileNames(pattern));
            foreach (string s in fileList)
            {
                ItemStructure item = new ItemStructure();
                item.Name = s;
                item.Type = ItemType.File;
                listItem.Add(item);
            }

            

            // Loop through the subdirectories, collect matches, 
            // and make separators consistent. 
            //foreach (string directory in GetAllDirectories("*", storeFile))
            //{
            //    foreach (string file in storeFile.GetFileNames(directory + "/" + fileString))
            //    {
            //        fileList.Add((directory + "/" + file));
            //    }
            //}

            //return fileList;
        }

        /*
        private void listFile_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
           
        }

        
        
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            

            List<ItemStructure> listItem1 = new List<ItemStructure>();
            GetPattern("*", isoStore, listItem1);

            ItemStructure item1 = new ItemStructure();
            item1.Name = "file99.txt";
            item1.Type = ItemType.File;
            listItem1.Add(item1);
            listFile.ItemsSource = listItem1;

        }

        private void listFile_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {

        }

        
        private void listFile_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {            
            //Item selected
            ItemStructure item = (ItemStructure)listFile.SelectedItem;

            //get list item
            

            try{
                
                // check if file return
                if (item.Type == ItemType.File)
                {
                    return;
                }

                // check if Up
                if (item.Type == ItemType.Up)
                {                   
                    // find last occurence of slash character and cut the string
                    int pos = curDir.LastIndexOf("\\");
                    if (pos == -1)
                    {
                        curDir = "";
                    }
                    else
                    {
                        curDir = curDir.Substring(0, pos);
                    }
                    
                } 
                
                // check if folder
                if (item.Type == ItemType.Folder) {
                    //
                    curDir = Path.Combine(curDir, item.Name);                    
                }
                
                //
                listItem.Clear();
                GetPattern(Path.Combine(curDir, "*"), isoStore, listItem);

                //
                listFile.ItemsSource = null;
                listFile.ItemsSource = listItem;
            }
            catch (Exception)
            {
            
            }

            


            //MessageBox.Show(Path.Combine(item.Name, "*"));
            
            
        } // End of GetFiles.
        */
        #endregion
    }
}